package com.hl.controller;

import jakarta.validation.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
//@Validated 注解很重要，如果没有它，参数校验不会生效
@Validated
public class ControllerTest {
    private static final Logger LOG = LoggerFactory.getLogger(ControllerTest.class);

    @GetMapping("get")
    public String get(@NotBlank(message = "name 不能为空") String name, int age) {
        LOG.info("name:" + name + "\tage:" + age);
        return "success";
    }

    @PostMapping("post")
    public String put(@RequestBody @Validated Body body) {
        LOG.info("body:" + body.toString());
        return "success";
    }
}
